<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="线索编号" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入线索编号" />
                </el-form-item>
                <el-form-item label="联系人" prop="contactName">
                    <el-input v-model="formData.contactName" placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="客户名称" prop="customerName">
                    <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
                </el-form-item>
                <el-form-item label="电话" prop="number">
                    <el-input-number v-model="formData.number" :max="9999" />
                </el-form-item>
                <el-form-item label="手机" prop="phone">
                    <el-input-number v-model="formData.phone" :max="9999" />
                </el-form-item>
                <el-form-item label="线索类型" prop="type">
                    <el-select class="flex-1" v-model="formData.type" placeholder="请选择线索类型">
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="线索状态" prop="status">
                    <el-select
                        class="flex-1"
                        v-model="formData.status"
                        placeholder="请选择线索状态"
                    >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="下次跟进时间" prop="nextFollowUpTime">
                    <el-input
                        v-model="formData.nextFollowUpTime"
                        placeholder="请输入下次跟进时间"
                    />
                </el-form-item>
                <el-form-item label="前业务人员" prop="formerBusinessPersonnelId">
                    <el-input
                        v-model="formData.formerBusinessPersonnelId"
                        placeholder="请输入前业务人员"
                    />
                </el-form-item>
                <el-form-item label="前业务人员名称" prop="formerBusinessPersonnelName">
                    <el-select
                        class="flex-1"
                        v-model="formData.formerBusinessPersonnelName"
                        placeholder="请选择前业务人员名称"
                    >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { cluepoolEdit, cluepoolAdd, cluepoolDetail } from '@/api/cluepool'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑线索池zsgc' : '新增线索池zsgc'
})

const formData = reactive({
    id: '',
    contactName: '',
    customerName: '',
    number: 0,
    phone: 0,
    type: '',
    status: '',
    nextFollowUpTime: '',
    formerBusinessPersonnelId: '',
    formerBusinessPersonnelName: ''
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入线索编号',
            trigger: ['blur']
        }
    ],
    contactName: [
        {
            required: true,
            message: '请输入联系人',
            trigger: ['blur']
        }
    ],
    customerName: [
        {
            required: true,
            message: '请输入客户名称',
            trigger: ['blur']
        }
    ],
    number: [
        {
            required: true,
            message: '请输入电话',
            trigger: ['blur']
        }
    ],
    phone: [
        {
            required: true,
            message: '请输入手机',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择线索类型',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择线索状态',
            trigger: ['blur']
        }
    ],
    nextFollowUpTime: [
        {
            required: true,
            message: '请输入下次跟进时间',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await cluepoolEdit(data) : await cluepoolAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await cluepoolDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
